
package com.lht.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lht.admin.annotation.SysLog;
import com.lht.admin.constants.DataValidConstant;
import com.lht.admin.entity.ProductEntity;
import com.lht.admin.entity.SysUserEntity;
import com.lht.admin.service.ProductService;
import com.lht.admin.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 贷款产品表Controller
 *
 * @author ZhengZhanWu
 * @date 2020-01-06 23:44:52
 */
@RestController
@RequestMapping("/product")
public class ProductController extends AbstractController {
    @Autowired
    private ProductService productService;

    /**
     * 查看所有列表
     *
     * @param params 查询参数
     * @return R
     */
    @RequestMapping("/queryAll")
    @RequiresPermissions("product:list")
    public R queryAll(@RequestParam Map<String, Object> params) {
        List<ProductEntity> list = productService.queryAll(params);

        return R.ok().put("list", list);
    }

    /**
     * 分页查询贷款产品表
     *
     * @param params 查询参数
     * @return R
     */
    @GetMapping("/list")
    @RequiresPermissions("product:list")
    public R list(@RequestParam Map<String, Object> params) {
        Page page = productService.queryPage(params);

        return R.ok().put("page", parseToPageUtils(page));
    }

    /**
     * 根据主键查询详情
     *
     * @param id 主键
     * @return R
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("product:info")
    public R info(@PathVariable("id") Long id) {
        ProductEntity product = productService.getById(id);

        return R.ok().put("product", product);
    }

    /**
     * 新增贷款产品表
     *
     * @param product product
     * @return R
     */
    @SysLog("新增贷款产品表")
    @RequestMapping("/save")
    @RequiresPermissions("product:save")
    public R save(@RequestBody ProductEntity product) {
        SysUserEntity user = getUser();

        product.setCreateUserId(user.getUserId());
        product.setCreateAt(new Date());
        product.setUpdateUserId(user.getUserId());
        product.setUpdateAt(new Date());
        product.setIsDelete(DataValidConstant.IS_DELETE_FALSE_STR);

        productService.add(product);

        return R.ok();
    }

    /**
     * 修改贷款产品表
     *
     * @param product product
     * @return R
     */
    @SysLog("修改贷款产品表")
    @RequestMapping("/update")
    @RequiresPermissions("product:update")
    public R update(@RequestBody ProductEntity product) {

        SysUserEntity user = getUser();

        product.setUpdateUserId(user.getUserId());
        product.setUpdateAt(new Date());
        productService.update(product);

        return R.ok();
    }

    /**
     * 根据主键删除贷款产品表
     *
     * @param ids ids
     * @return R
     */
    @SysLog("删除贷款产品表")
    @RequestMapping("/delete")
    @RequiresPermissions("product:delete")
    public R delete(@RequestBody Long[] ids) {
        productService.deleteBatch(ids);

        return R.ok();
    }


    @RequestMapping("/uploadFile")
    public R uploadFile(MultipartFile file) {
        String url = productService.uploadFile(file, getUserId());
        R r = new R();
        r.put("url", url);
        r.put("link", url);
        return r;
    }


    @RequestMapping("/importData")
    @RequiresPermissions("product:importData")
    public R importData(MultipartFile file) {
        return productService.importData(file, getUser());
    }
}
